package com.devilvan.betrice.aiqicha.deserialize;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Evad.Wu
 * @Description 爱企查 自定义JSON反序列化 '-'值转为空
 * @date 2022-09-24
 */
@SuppressWarnings(value = "all")
public class ParseHyphenDeserializer implements ObjectDeserializer {
    private static final String HYPHEN = "-";

    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        String field = (String) parser.parse();
        if (HYPHEN.equals(field) || "".equals(field)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(field);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (T) date;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
